﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using SharedLibraries;
using SharedLibraries.Constants;
using ThangNM_LIB.EO;
using ThangNM_LIB.FUNC;

namespace ThangNM.UserControl
{
    public partial class Topic_List_ShowUC : System.Web.UI.UserControl
    {
        public Int32 PageNumber
        {
            get { return (ViewState["PageNumber"] == null ? 1 : (Int32)ViewState["PageNumber"]); }
            set { ViewState["PageNumber"] = value; }
        }
        public String sUrlNavigator
        {
            get { return (String)ViewState["sUrlNavigator"]; }
            set { ViewState["sUrlNavigator"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void setTitle(String sTitle)
        {
            lblTitle.Text = sTitle;
        }

        public void BindData(DataTable dt, SEOEO _SEOEO)
        {
            if (_SEOEO == null || _SEOEO.PK_iSEO <= 0)
                divBC.Visible = false;
            else
            {
                divBC.Visible = true;
                Commons.setBreadcrumb(_SEOEO, lblBreadcrumb);
            }
            dt.Columns.Add(new DataColumn("UrlAuthor", Type.GetType("System.String")));
            dt.Columns.Add(new DataColumn("sDateTimeCreate", Type.GetType("System.String")));
            foreach (DataRow dr in dt.Rows)
            {
                dr["sUrlRewrite"] = OtherEx.AddDomainInUrl(Convert.ToString(dr["sUrlRewrite"]));
                dr["UrlAuthor"] = OtherEx.AddDomainInUrl(FileNameByFunction.Tac_Gia_ + Convert.ToString(dr["FK_sAccount"]) + FileNameByFunction.HTML);
                dr["sDateTimeCreate"] = Convert.ToDateTime(dr["tDateTimeCreate"]).ToString(FormatEx.Date_Time.Format_DateTime_WebView);
            }
            PagedDataSource pgitems = new PagedDataSource();
            System.Data.DataView dv = new System.Data.DataView(dt);
            pgitems.DataSource = dv;
            pgitems.AllowPaging = true;
            Int32 PageSize = 5;
            Int32.TryParse(Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["pagesize"]), out PageSize);
            pgitems.PageSize = PageSize;
            pgitems.CurrentPageIndex = PageNumber - 1;
            if (pgitems.PageCount > 1)
            {
                rptPages.Visible = true;
                System.Collections.ArrayList pages = new System.Collections.ArrayList();
                for (int i = 0; i < pgitems.PageCount; i++)
                    pages.Add((i + 1).ToString());
                rptPages.DataSource = pages;
                rptPages.DataBind();
            }
            else
                rptPages.Visible = false;
            rptTopic.DataSource = pgitems;
            rptTopic.DataBind();
        }

        protected void rptPages_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            HyperLink hplPage = (HyperLink)e.Item.FindControl("hplPage");
            if (hplPage != null)
            {
                hplPage.NavigateUrl = OtherEx.AddDomainInUrl(StringEx.Trim(sUrlNavigator) + ConstEx.UrlEX_C.Trang + hplPage.Text);
                if (Convert.ToInt32(hplPage.Text) == PageNumber)
                {
                    hplPage.Enabled = false;
                    hplPage.BackColor = System.Drawing.Color.Orange;
                }
            }
        }

        protected void rptTopic_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            //try
            //{
            //    if (e.CommandName == "Like")
            //    {
            //        LinkButton lbtniLike = (LinkButton)e.Item.FindControl("lbtniLike");
            //        Literal lbliLike = (Literal)e.Item.FindControl("lbliLike");
            //        lbtniLike.Enabled = false;
            //        lbliLike.Text = (Convert.ToInt32(lbliLike.Text) + 1).ToString();
            //        TopicEO _TopicEO = new TopicEO();
            //        _TopicEO.PK_iTopicID = Convert.ToInt32(((HiddenField)e.Item.FindControl("hfID")).Value);
            //        _TopicEO.iLike = Convert.ToInt32(lbliLike.Text);
            //        TopicDAO.Update_iVisit_Or_iLike(_TopicEO);
            //    }
            //}
            //catch (Exception ex)
            //{
            //MsgUC1.setNotifications(ConstEx.FunctionSetMsg.ERROR, ex.Message);
            //Commons.WriteLogError(ex.ToString());
            //}
        }

        protected void rptTopic_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView drv = (DataRowView)e.Item.DataItem;
                Repeater rptTag = (Repeater)e.Item.FindControl("rptTag");
                Commons.setTags(rptTag, Convert.ToString(drv["FK_sCategory"]));
            }
        }
    }
}